package com.itheima.web;

import com.itheima.goods.pojo.Goods;
import com.itheima.service.GoodsService;
import entity.Result;
import org.junit.runners.Parameterized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {
    //注入业务层对象
    @Autowired
    private GoodsService goodsService;

    //查询所有商品信息
    @GetMapping
    public Result findAll() throws IOException {
        //调用业务层查询方法
        return goodsService.findAll();
    }

    //添加商品
    @PostMapping
    public Result add(Goods goods){
        //调用业务层添加商品方法
        return goodsService.add(goods);
    }

    //分页查询
    @GetMapping("/search/{page}/{size}")
    public Result findPage(@PathVariable("page")Integer page,@PathVariable("size")Integer size,@RequestParam("searchMap") Goods goods) throws IOException {
        //调用业务层分页查询方法
        return goodsService.findPage(page,size,goods);
    }


    //根据商品id查询
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        //调用业务层查询方法
        return goodsService.findById(id);
    }

    //根据id修改商品信息
    @PutMapping("/{id}")
    public Result update(@PathVariable("id")Integer id,@RequestParam("pojo") Goods goods){
        //调用业务层修改方法
        return goodsService.update(id,goods);
    }

    //根据id修改商品信息
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")Integer id){
        //调用业务层删除方法
        return goodsService.delete(id);
    }



}
